
#ifndef TREEMODEL_H
#define TREEMODEL_H

#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QThreadPool>

#include "tasktree.h"

class TreeItem;

class QTimer;

enum class FlushSpeed
{
    High, Normal, Slow,
};

class TreeModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    explicit TreeModel(TaskTree *taskTree, QObject *parent = 0);
    ~TreeModel();

    QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
    int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;

    void sort(int column, Qt::SortOrder order) Q_DECL_OVERRIDE;
    bool event(QEvent *) Q_DECL_OVERRIDE;

    bool isAutoFlush() const;
    QModelIndex getIndexByPid(pid_t pid) const;

    int getSortColumn() const { return m_sort_column; }

    Qt::SortOrder getSortOrder() const { return m_sort_order; }

signals:
    void beforeSort();

    void afterSort();

public slots:

    void flush();

    void setFlushSpeed(FlushSpeed speed);

    void setTreeMode(bool treeMode);

    void setAutoFlush(bool autoFlush);

private:

    void _remove_items();

    void _insert_new_items();

    static QString _prop_name(TIP prop);

    TaskTree *m_tasktree;
    QTimer *m_timer;

    QThreadPool *m_threadpool;

    int m_sort_column = 0;
    Qt::SortOrder m_sort_order = Qt::AscendingOrder;
};

#endif // TREEMODEL_H
